﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports usshare

Public Class urlshorten
    Inherits servelib

    Public Const _pathidentity As String = "/"
    Private previewRedirector As Boolean
    Private adminlink As String = Nothing
    Private adminmail As String = Nothing
    Private visitCounterHomepage As String = Nothing
    Private visitCounterLink As String = Nothing
    Private ieClipboardPrompt As String = Nothing
#If RETIRED Then
    'make the text / link related to language
    Private othertexts As vector(Of String) = Nothing
    Private otherlinks As vector(Of String) = Nothing
#End If
    Private snapshotLink As String = Nothing
    Private snapshotHomepage As String = Nothing
    Private keyVisitCounterHomepage As String = Nothing
    Private promotedDomain As String = Nothing
    Private promotedHost As String = Nothing
    Private usefasterLink As String = Nothing
    Private config As configParser = Nothing

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function enableEmptyControl(ByVal context As httpContext) As Boolean
        'widen the requirement, as cannot decide whether is delayjumpMode here.
        Return Not previewRedirector OrElse Not context.isBrowserUserAgent()
    End Function

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        assert(Not config Is Nothing, "urlshorten should be initialized with a valid configParser.")

        MyBase.initial(config)
        previewRedirector = config.parameter(configNaming.urlshorten.name _
                                              , configNaming.urlshorten.previewRedirector _
                                              , configNaming.defaultValue.urlshorten.previewRedirector)
        adminlink = config.parameter(configNaming.urlshorten.name _
                                      , configNaming.urlshorten.adminlink _
                                      , configNaming.defaultValue.urlshorten.adminlink)
        adminmail = config.parameter(configNaming.urlshorten.name _
                                      , configNaming.urlshorten.adminmail _
                                      , configNaming.defaultValue.urlshorten.adminmail)
        visitCounterHomepage = config.parameter(configNaming.urlshorten.name _
                                                 , configNaming.urlshorten.visitCounterHomepage _
                                                 , configNaming.defaultValue.urlshorten.visitCounterHomepage)
        visitCounterLink = config.parameter(configNaming.urlshorten.name _
                                             , configNaming.urlshorten.visitCounterLink _
                                             , configNaming.defaultValue.urlshorten.visitCounterLink)
        ieClipboardPrompt = config.parameter(configNaming.urlshorten.name _
                                              , configNaming.urlshorten.ieClipboardPromptLink _
                                              , configNaming.defaultValue.urlshorten.ieClipboardPromptLink)
#If RETIRED Then
        config.readlist(configNaming.urlshorten.name, configNaming.urlshorten.otherlink, otherlinks)
        config.readlist(configNaming.urlshorten.name, configNaming.urlshorten.othertext, othertexts)
#End If
        snapshotLink = config.parameter(configNaming.urlshorten.name, _
                                        configNaming.urlshorten.snapshotLink, _
                                        configNaming.defaultValue.urlshorten.snapshotLink)
        snapshotHomepage = config.parameter(configNaming.urlshorten.name, _
                                            configNaming.urlshorten.snapshotHomepage, _
                                            configNaming.defaultValue.urlshorten.snapshotHomepage)
        keyVisitCounterHomepage = config.parameter(configNaming.urlshorten.name, _
                                                   configNaming.urlshorten.keyVisitCounterHomepage, _
                                                   configNaming.defaultValue.urlshorten.keyVisitCounterHomepage)
        promotedDomain = config.parameter(configNaming.urlshorten.name, _
                                          configNaming.urlshorten.promotedDomain, _
                                          configNaming.defaultValue.urlshorten.promotedDomain)
        urlUtils.splitUrl(promotedDomain, Nothing, Nothing, Nothing, promotedHost, Nothing, _
                          CType(Nothing, String), Nothing, Nothing, Nothing)
        usefasterLink = config.parameter(configNaming.urlshorten.name, _
                                          configNaming.urlshorten.usefasterLink, _
                                          configNaming.defaultValue.urlshorten.usefasterLink)

        Me.config = config
    End Sub

    Private Sub autoclipboardWriterInvoke(ByVal page As xhtmlpage, ByVal key As String)
        autoclipboardTextWriter.invoke(page, "'" + urlgenerator.jumpfull(key) + "'")
    End Sub

    Protected Overrides Function jump(ByVal context As httpContext, ByRef control As xmlcontrol _
                                      , ByVal key2urlResult As Boolean _
                                      , ByVal key As String, ByVal url As String, ByVal delay As Int64) As Boolean
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.title().text = context.string(stringId.JUMP_TITLE)

        If Not key2urlResult Then
            key_nofound(context, page, key)
        Else
            Dim delayJumpMode As Boolean
            delayJumpMode = delay > 0
            'when the requester is a browser and have no referer, use page redirector, otherwise 30x redirect
            'a little dirty here, but delayJumpMode & direct jump uses same code to set title / delayjump
            If previewRedirector OrElse delayJumpMode OrElse _
                (context.isBrowserUserAgent() AndAlso isemptystring(context.referer())) Then
                If previewRedirector OrElse delayJumpMode Then
                    assert(preview(context, page, key2urlResult, key, url) _
                           , "should not failed to preview if key2urlResult is true.")
                    page.body().addchild(context.createControl(New br()))
                    Dim xmltext As xmltext = Nothing
                    xmltext = context.createControl(New xmltext())
                    xmltext.text = context.string(stringId.JUMP_AUTOJUMP_BEFOREDELAY) + _
                                   Convert.ToString(delay) + context.string(stringId.JUMP_AUTOJUMP_AFTERDELAY)
                    page.body().addchild(xmltext)
                Else
                    Dim a As a = Nothing
                    a = context.createControl(New a())
                    a.text = context.string(stringId.JUMP_AUTOJUMP_NOPREVIEW) + url
                    a.href = url
                    page.body().addchild(a)
                    page.body().addchild(context.createControl(New br()))

                    keyVisitCounter(context, page.body(), key)
                End If

                page.title().text = context.string(stringId.JUMP_TITLE_PAGEJUMP)

                Dim delayjump As delayJump = Nothing
                delayjump = context.createControl(New delayJump())
                delayjump.delay = delay
                delayjump.destination = url
                page.head().addchild(delayjump)
            Else
                'clear the control first, so controlProcessor can handle it, as already set enableEmptyControl
                page = Nothing
                pingVisitCount(key)

                context.redirect(url)
            End If
        End If

        control = page
        Return True
    End Function

    Private Sub keyVisitCounter(ByVal context As httpContext, ByVal root As xmlcontrol, ByVal key As String)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.href = keyVisitCounterHomepage
        root.addchild(a)

        Dim xmltext As xmltext = Nothing
        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.KEYVISITCOUNTER_BEFOREIMG)
        a.addchild(xmltext)

        Dim img As img = Nothing
        img = context.createControl(New img())
        img.src = generateVisitCountLink(key)
        img.style = "border:0;"
        img.height = "16px"
        a.addchild(img)

        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.KEYVISITCOUNTER_AFTERIMG)
        a.addchild(xmltext)
    End Sub

    Protected Overrides Function preview(ByVal context As httpContext, ByRef control As xmlcontrol _
                                         , ByVal key2urlResult As Boolean _
                                         , ByVal key As String, ByVal url As String) As Boolean
        assert(Not context Is Nothing, "context is nothing.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.title().text = context.string(stringId.PREVIEW_TITLE)
        'avoid any change to color.windowframe, and this page has no css
        page.body().style = "background:" + Convert.ToString(color.windowframe) + ";"

        If key2urlResult Then
            Dim ul As ul = Nothing
            ul = context.createControl(New ul())
            page.body().addchild(ul)

            Dim li As li = Nothing
            li = context.createControl(New colorli(page.inlineScript()))
            ul.addchild(li)

            Dim xmltext As xmltext = Nothing
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.PREVIEW_KEY_PROVIDE) + key + context.string(stringId.COMMA)
            li.addchild(xmltext)
            keyVisitCounter(context, li, key)

            li = context.createControl(New colorli(page.inlineScript()))
            ul.addchild(li)

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.PREVIEW_ACCEPT_JUMP_FORMS)
            li.addchild(xmltext)
            li.addchild(context.createControl(New br()))

            Dim a As a = Nothing
            a = context.createControl(New a())
            a.text = urlgenerator.jumpfull(key)
            a.href = Convert.ToString(a.text)
            li.addchild(a)
            li.addchild(context.createControl(New br()))

            a = context.createControl(New a())
            a.text = urlgenerator.jump(key)
            a.href = Convert.ToString(a.text)
            li.addchild(a)
            li.addchild(context.createControl(New br()))

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.PREVIEW_ACCEPT_DELAYJUMP_FORM)
            li.addchild(xmltext)
            li.addchild(context.createControl(New br()))

            a = context.createControl(New a())
            a.text = urlgenerator.delayjump(key)
            a.href = Convert.ToString(a.text)
            li.addchild(a)

            li = context.createControl(New colorli(page.inlineScript()))
            If shortenEffect(context, li, key, url) Then
                ul.addchild(li)
            End If

            li = context.createControl(New colorli(page.inlineScript()))
            ul.addchild(li)

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.PREVIEW_AUTOJUMP_TO)
            li.addchild(xmltext)

            a = context.createControl(New a())
            a.text = url
            a.href = url
            li.addchild(a)

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.PREVIEW_IN_JUMP_OR_DELAYJUMP_MODE)
            li.addchild(xmltext)

            li = context.createControl(New colorli(page.inlineScript()))
            ul.addchild(li)

            a = context.createControl(New a())
            a.href = snapshotHomepage
            li.addchild(a)

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.PREVIEW_SNAPSHOT)
            a.addchild(xmltext)
            a.addchild(context.createControl(New br()))

            Dim img As img = Nothing
            img = context.createControl(New img())
            img.src = generateSnapshotLink(snapshotLink, url)
            img.style = "border:0;"
            a.addchild(img)

            li = context.createControl(New colorli(page.inlineScript()))
            ul.addchild(li)

            a = context.createControl(New a())
            a.text = context.string(stringId.GOBACK_HOMEPAGE)
            a.href = network.thislevelPathMark
            li.addchild(a)
        Else
            key_nofound(context, page, key)
        End If

        control = page
        Return True
    End Function

    Protected Overrides Function submit(ByVal context As httpContext, ByRef control As xmlcontrol _
                                        , ByVal submitResult As submitResult _
                                        , ByVal key As String, ByVal url As String) As Boolean
        assert(Not context Is Nothing, "context is nothing.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.title().text = context.string(stringId.SUBMIT_TITLE)

        If submitResult = submitResult.invalidKey Then
            Dim xmltext As xmltext = Nothing
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SUBMIT_KEY_SUBMITTED) + key + _
                           context.string(stringId.SUBMIT_KEY_INVALID)
            page.body().addchild(xmltext)
            page.body().addchild(context.createControl(New br()))

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SUBMIT_KEY_TRY_ANOTHER)
            page.body().addchild(xmltext)
            page.body().addchild(context.createControl(New br()))

            delayJump_homepage(context, page)
        ElseIf submitResult = submitResult.existingKey Then
            Dim xmltext As xmltext = Nothing
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SUBMIT_KEY_SUBMITTED) + key + _
                           context.string(stringId.SUBMIT_KEY_REGISTERED)
            page.body().addchild(xmltext)
            page.body().addchild(context.createControl(New br()))

            delayJump_preview(context, page, key)
        ElseIf submitResult = submitResult.internalError Then
            Dim xmltext As xmltext = Nothing
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SUBMIT_INTERNAL_ERROR) + key + _
                           context.string(stringId.SUBMIT_TO_URL) + url
            page.body().addchild(xmltext)
            page.body().addchild(context.createControl(New br()))

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.TRY_AGAIN_LATER)
            page.body().addchild(xmltext)
            page.body().addchild(context.createControl(New br()))

            delayJump_homepage(context, page)
        ElseIf submitResult = submitResult.success Then
            Dim xmltext As xmltext = Nothing
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SUBMIT_SUCCESS) + key + context.string(stringId.SUBMIT_TO_URL) + url
            page.body().addchild(xmltext)
            page.body().addchild(context.createControl(New br()))
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SUBMIT_WRITEFULLURL)
            page.body().addchild(xmltext)
            Dim a As a = Nothing
            a = context.createControl(New a())
            Dim fullurl As String = Nothing
            fullurl = urlgenerator.jumpfull(key)
            a.text = fullurl
            a.href = fullurl
            page.body().addchild(a)
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SUBMIT_TOCLIPBOARD)
            page.body().addchild(xmltext)
            page.body().addchild(context.createControl(New br()))

            shortenEffect(context, page.body(), key, url)
            page.body().addchild(context.createControl(New br()))

            delayJump_preview(context, page, key)

            page.body().addchild(context.createControl(New br()))
            a = context.createControl(New a())
            a.text = context.string(stringId.GOBACK_HOMEPAGE)
            a.href = network.thislevelPathMark
            page.body().addchild(a)

            autoclipboardWriterInvoke(page, key)
        Else
            assert(False, "should cover all submitResults already.")
        End If

        control = page
        Return True
    End Function

    Protected Overrides Function urlkeys(ByVal context As httpContext, ByRef control As xmlcontrol _
                                         , ByVal url2keysResult As Boolean _
                                         , ByVal url As String, ByVal keys As [set](Of String)) As Boolean
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.title().text = context.string(stringId.URLKEYS_TITLE)

        If url2keysResult Then
            Dim xmltext As xmltext = Nothing
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.URLKEYS_URL)
            page.body().addchild(xmltext)

            Dim a As a = Nothing
            a = context.createControl(New a())
            a.text = url
            a.href = url
            page.body().addchild(a)

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.URLKEYS_RELATED)
#If RETIRED Then
            If keys.size() > 1 Then
                xmltext.text += "s"
            End If
            xmltext.text += "."
#End If
            page.body().addchild(xmltext)

            Dim ul As ul = Nothing
            ul = context.createControl(New ul())
            page.body().addchild(ul)

            Dim it As [set](Of String).iterator = Nothing
            it = keys.begin()
            While it <> keys.end()
                Dim li As li = Nothing
                li = context.createControl(New colorli(page.inlineScript()))
                ul.addchild(li)

                xmltext = context.createControl(New xmltext())
                xmltext.text = (+it) + character.blank + character.minusSign + character.blank
                li.addchild(xmltext)

                a = context.createControl(New a())
                a.text = context.string(stringId.PREVIEW)
                a.href = urlgenerator.preview(+it)
                li.addchild(a)

                xmltext = context.createControl(New xmltext())
                xmltext.text = character.blank + character.minusSign + character.blank
                li.addchild(xmltext)

                a = context.createControl(New a())
                a.text = context.string(stringId.JUMP_SHORTFORM)
                a.href = urlgenerator.jumpfull(+it)
                li.addchild(a)

                xmltext = context.createControl(New xmltext())
                xmltext.text = character.blank + character.minusSign + character.blank
                li.addchild(xmltext)

                a = context.createControl(New a())
                a.text = context.string(stringId.JUMP_LONGFORM)
                a.href = urlgenerator.jump(+it)
                li.addchild(a)

                xmltext = context.createControl(New xmltext())
                xmltext.text = character.blank + character.minusSign + character.blank
                li.addchild(xmltext)

                a = context.createControl(New a())
                a.text = context.string(stringId.DELAYJUMP)
                a.href = urlgenerator.delayjump(+it)
                li.addchild(a)

                it += 1
            End While

            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.URLKEYS_FIRSTURLTOCLIPBOARD)
            page.body().addchild(xmltext)

            page.body().addchild(context.createControl(New br()))
            a = context.createControl(New a())
            a.text = context.string(stringId.GOBACK_HOMEPAGE)
            a.href = network.thislevelPathMark
            page.body().addchild(a)

            autoclipboardWriterInvoke(page, +(keys.begin()))
        Else
            urlkey_notfound(context, page, url)
        End If

        control = page
        Return True
    End Function

    'do not need to care about this, as there is no any entry to a user to this page.
    'just leave it here to avoid break servelib logic.
    Protected Overrides Function have(ByVal context As httpContext, ByRef control As xmlcontrol _
                                      , ByVal haveResult As Boolean, ByVal url As String) As Boolean
        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.title().text = context.string(stringId.HAVE_TITLE)

        If haveResult Then
            page.text = context.string(stringId.HAVE_YES_BEFORE_URL) + url + context.string(stringId.HAVE_YES_AFTER_URL)
        Else
            page.text = context.string(stringId.HAVE_NO) + url
        End If

        control = page
        Return True
    End Function

    Private Sub homepage_rightrail(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing)
        assert(Not root Is Nothing)
        Dim div As div = Nothing
        div = context.createControl(New div())
        div.text = String.Format(context.string(stringId.HOMEPAGE_URLCOUNT), Convert.ToString(recordCount))
        root.addchild(div)

        div = context.createControl(New div())
        div.id = "disclaimer"
        div.text = context.string(stringId.HOMEPAGE_DISCLAIMER)
        root.addchild(div)

        div = context.createControl(New div())
        div.text = context.string(stringId.HOMEPAGE_VISITCOUNTER)
        root.addchild(div)

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.href = visitCounterHomepage
        a.class = "c_vc"
        div.addchild(a)

        Dim img As img = Nothing
        img = context.createControl(New img())
        img.src = visitCounterLink
        img.height = "26px"
        img.alt = div.text
        img.title = div.text
        a.addchild(img)
    End Sub

    Protected Overrides Function homepage(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")

        Dim page As xhtmlpage = Nothing
        page = generateXhtmlPage(context)

        page.addcsslink(cssrequest.generateCssfileLinkUrl("urlshorten.css"))
        page.title().text = context.string(stringId.HOMEPAGE_TITLE)
        'avoid any change to color.windowframe, so leave it here
        page.body().style = "background:" + Convert.ToString(color.windowframe) + ";"

        Dim meta As meta = Nothing
        meta = context.createControl(New meta())
        meta.name() = "keywords"
        meta.content() = "urlshorten url-shorten"
        page.head().addchild(meta)

        meta = context.createControl(New meta())
        meta.name() = "description"
        meta.content() = "a simple and quick url-shorten service owned by Hzj_jie, powered by " + _
                         applicationName() + " + storoom, served via frontdoor."
        page.head().addchild(meta)

        Dim label As label = Nothing
        label = context.createControl(New label())
        If fitfilter("*zmd.be", context.url().host()) Then
            label.text = context.string(stringId.BRAND_ZMD)
        ElseIf fitfilter("*rlss.in", context.url().host()) Then
            label.text = context.string(stringId.BRAND_RLSS)
        Else
            label.text = context.string(stringId.BRAND)
        End If
        label.id = "id_bnd"
        page.body().addchild(label)

        Dim a As a = Nothing
        If Not strsame(context.url().host(), promotedHost, False) Then
            a = context.createControl(New a())
            a.id = "prmt"
            a.text = context.string(stringId.PROMOTEDDOMAIN)
            a.href = promotedDomain
            page.body().addchild(a)
        End If

        label = context.createControl(New label())
        label.text = context.string(stringId.HOMEPAGE_WELCOME_TO_USE)
        page.body().addchild(label)

        label = context.createControl(New label())
        label.text = context.string(stringId.HOMEPAGE_INTRODUCTION)
        page.body().addchild(label)

        Dim content As div = Nothing
        content = context.createControl(New div())
        content.id = "cont"
        page.body().addchild(content)

        Dim div As div = Nothing
        div = context.createControl(New div())
        div.id = "lr"
        content.addchild(div)

        Dim ul As ul = Nothing
        ul = context.createControl(New ul())
        div.addchild(ul)

        Dim li As li = Nothing
        li = context.createControl(New colorli(page.inlineScript()))
        li.text = context.string(stringId.HOMEPAGE_ADD_MAPPING)
        ul.addchild(li)
        homepage_submit(context, page, li)

        li = context.createControl(New colorli(page.inlineScript()))
        li.text = context.string(stringId.HOMEPAGE_PREVIEW)
        ul.addchild(li)
        homepage_preview(context, li)

        li = context.createControl(New colorli(page.inlineScript()))
        li.text = context.string(stringId.HOMEPAGE_JUMP)
        ul.addchild(li)
        homepage_jump(context, li, False)

        li = context.createControl(New colorli(page.inlineScript()))
        li.text = context.string(stringId.HOMEPAGE_DELAYJUMP)
        ul.addchild(li)
        homepage_jump(context, li, True)

        li = context.createControl(New colorli(page.inlineScript()))
        li.text = context.string(stringId.HOMEPAGE_URLKEYS)
        ul.addchild(li)
        homepage_urlkey(context, li)

        li = context.createControl(New colorli(page.inlineScript()))
        li.text = context.string(stringId.HOMEPAGE_OTHERLINKS)
        ul.addchild(li)
        homepage_links(context, li)

        li = context.createControl(New colorli(page.inlineScript()))
        li.text = context.string(stringId.HOMEPAGE_OTHERLINKS2)
        ul.addchild(li)
        homepage_otherlinks(context, li)

        div = context.createControl(New div())
        div.id = "rr"
        content.addchild(div)
        homepage_rightrail(context, div)

        control = page
        Return True
    End Function

    Private Sub key_nofound(ByVal context As httpContext, ByVal page As xhtmlpage, ByVal key As String)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not page Is Nothing, "page is a nothing xhtmlpage instance.")

        page.body().text = context.string(stringId.KEY_NOFOUND) + key + context.string(stringId.FULLSTOP)
        delayJump_homepage(context, page)
    End Sub

    Private Function shortenEffect(ByVal context As httpContext, ByVal root As xmlcontrol _
                                   , ByVal key As String, ByVal url As String) As Boolean
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not root Is Nothing, "root is nothing.")

        Dim jumpUrl As String = Nothing
        jumpUrl = urlgenerator.jumpfull(key)
        If strlen(url) > strlen(jumpUrl) Then
            Dim xmltext As xmltext = Nothing
            xmltext = context.createControl(New xmltext())
            xmltext.text = context.string(stringId.SHORTEN_EFFECT_BEFORE) + _
                           Convert.ToString(strlen(url) - strlen(jumpUrl)) + _
                           context.string(stringId.SHORTEN_EFFECT_AFTER)
            root.addchild(xmltext)

            Return True
        Else
            Return False
        End If
    End Function

    Private Sub delayJump_homepage(ByVal context As httpContext, ByVal page As xhtmlpage)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not page Is Nothing, "page is a nothing xhtmlpage instance.")

        Dim xmltext As xmltext = Nothing
        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.DELAY_JUMP_BEFORE)
        page.body().addchild(xmltext)

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.href = network.thislevelPathMark
        a.text = context.string(stringId.DELAY_JUMP_HOMEPAGE_TITLE)
        page.body().addchild(a)

        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.DELAY_JUMP_AFTER)
        page.body().addchild(xmltext)

        Dim delayjump As delayJump = Nothing
        delayjump = context.createControl(New delayJump())
        delayjump.delay = 10
        delayjump.destination = network.thislevelPathMark
        page.head().addchild(delayjump)
    End Sub

    Private Sub delayJump_preview(ByVal context As httpContext, ByVal page As xhtmlpage, ByVal key As String)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not page Is Nothing, "page is a nothing xhtmlpage instance.")

        Dim xmltext As xmltext = Nothing
        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.DELAY_JUMP_BEFORE)
        page.body().addchild(xmltext)

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.href = urlgenerator.preview(key)
        a.text = context.string(stringId.DELAY_JUMP_PREVIEW_KEY)
        page.body().addchild(a)

        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.DELAY_JUMP_AFTER)
        page.body().addchild(xmltext)

        Dim delayjump As delayJump = Nothing
        delayjump = context.createControl(New delayJump())
        delayjump.delay = 10
        delayjump.destination = urlgenerator.preview(key)
        page.head().addchild(delayjump)
    End Sub

    Private Sub homepage_submit(ByVal context As httpContext, ByVal page As xhtmlpage, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not page Is Nothing, "page is a nothing xhtmlpage instance.")
        assert(Not root Is Nothing, "root is a nothing xmlcontrol instance.")

        Const clipboardTextName As String = "cptxt"
        Const urlinputId As String = "id_ui"
        Const fromClipboardId As String = "id_fcp"
        webctrl.var.invoke(page.inlineScript(), clipboardTextName, clipboardTextReader.invoke(page.inlineScript()))
        webctrl.ifelse.invoke(isurl.invoke(page.inlineScript(), clipboardTextName), _
                              changeValueById.invoke(page.inlineScript(), "'" + urlinputId + "'", clipboardTextName), _
                              hideById.invoke(page.inlineScript(), "'" + fromClipboardId + "'"), _
                              page.body().onload)

        Dim form As form = Nothing
        form = context.createControl(New form())
#If RETIRED Then
        form.id = "id_sbt_frm"
#End If
        form.action = network.thislevelPathMark
        form.method = xhtmlControl_1_1.method_enum.get
        root.addchild(form)

        Dim label As label = Nothing
        label = context.createControl(New label())
        label.id = "id_ul"
        label.text = context.string(stringId.HOMEPAGE_SUBMIT_URL)
        form.addchild(label)

        label = context.createControl(New label())
        label.id = fromClipboardId
        label.text = context.string(stringId.HOMEPAGE_SUBMIT_FROM_CLIPBOARD)
        form.addchild(label)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.url
        input.id = urlinputId
        input.value = network.httpAddressHeader
        input.class = "c_ui"
        form.addchild(input)

        label = context.createControl(New label())
#If RETIRED Then
        label.id = "id_sbt_kl"
#End If
        label.text = context.string(stringId.HOMEPAGE_SUBMIT_KEY)
        form.addchild(label)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.key
        input.value = generateRandomKey()
        input.class = "c_ki"
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.submit
        input.class = "c_s"
        input.value = context.string(stringId.HOMEPAGE_SUBMIT_SHORTIT)
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.hidden
        input.name = query.action
        input.value = actionVerbs(action.submit)
        form.addchild(input)
    End Sub

    Private Sub homepage_preview(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not root Is Nothing, "root is a nothing xmlcontrol instance.")

        Dim form As form = Nothing
        form = context.createControl(New form())
        form.id = "id_prv_frm"
        form.action = network.thislevelPathMark
        form.method = xhtmlControl_1_1.method_enum.get
        root.addchild(form)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.key
        input.class = "c_ki"
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.submit
        input.class = "c_s"
        input.value = context.string(stringId.PREVIEW)
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.hidden
        input.name = query.action
        input.value = actionVerbs(action.preview)
        form.addchild(input)
    End Sub

    Private Sub homepage_jump(ByVal context As httpContext, ByVal root As xmlcontrol, ByVal delayjump As Boolean)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not root Is Nothing, "root is a nothing xmlcontrol instance.")

        Dim cssname As String = Nothing
        If delayjump Then
            cssname = "djp"
        Else
            cssname = "jmp"
        End If

        Dim form As form = Nothing
        form = context.createControl(New form())
        form.id = "id_" + cssname + "_frm"
        form.action = network.thislevelPathMark
        form.method = xhtmlControl_1_1.method_enum.get
        root.addchild(form)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.key
        input.class = "c_ki"
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.submit
        input.class = "c_s"
        If delayjump Then
            input.value = context.string(stringId.DELAYJUMP)
        Else
            input.value = context.string(stringId.JUMP)
        End If
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.hidden
        input.name = query.action
        If delayjump Then
            input.value = actionVerbs(action.delayJump)
        Else
            input.value = actionVerbs(action.jump)
        End If
        form.addchild(input)
    End Sub

    Private Sub homepage_urlkey(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not root Is Nothing, "root is a nothing xmlcontrol instance.")

        Dim form As form = Nothing
        form = context.createControl(New form())
        form.id = "id_uk_frm"
        form.action = network.thislevelPathMark
        form.method = xhtmlControl_1_1.method_enum.get
        root.addchild(form)

        Dim input As input = Nothing
        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.text
        input.name = query.url
        input.value = network.httpAddressHeader
        input.class = "c_ui"
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.submit
        input.class = "c_s"
        input.value = context.string(stringId.SEARCH)
        form.addchild(input)

        input = context.createControl(New input())
        input.type = xhtmlctrl.input.type_enum.hidden
        input.name = query.action
        input.value = actionVerbs(action.urlkeys)
        form.addchild(input)
    End Sub

    Private Sub homepage_links(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not root Is Nothing, "root is a nothing xmlcontrol instance.")

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_IE8ACC)
        a.class = "c_lnks"
        a.href = strmid(ie8accInstaller._pathidentity, strlen(network.pathSeperator))
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_CLOSE_CLIPBOARD)
        a.class = "c_lnks"
        a.href = ieClipboardPrompt
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_USE_FASTER)
        a.class = "c_lnks"
        a.href = usefasterLink
        root.addchild(a)

        a = context.createControl(New a())
        Dim xmltext As xmltext = Nothing
        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.HOMEPAGE_USCLIENT)
        a.addchild(xmltext)
        Dim img As img = Nothing
        img = context.createControl(New img())
        img.src = staticrequest.generateStaticfileLinkUrl("acc.ico")
        img.width = "32px"
        img.height = "32px"
        img.class = "c_fr"
        a.addchild(img)
        a.class = "c_lnks"
        a.href = usclientDownloadLink()
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_WMUSCLNT)
        a.class = "c_lnks"
        a.href = wmusclntDownloadLink()
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_XMLAPI)
        a.href = strmid(xmlapi2._pathidentity, strlen(network.pathSeperator))
        a.class = "c_lnks"
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_TXTAPI)
        a.href = strmid(txtapi._pathidentity, strlen(network.pathSeperator))
        a.class = "c_lnks"
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_IDCARD)
        a.href = strmid(idcard._pathidentity, strlen(network.pathSeperator))
        a.class = "c_lnks"
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_README)
        a.href = staticrequest.generateStaticfileLinkUrl("readme.txt")
        a.class = "c_lnks"
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_TOPHOMEPAGE_BEFORE) + adminlink + _
                 context.string(stringId.HOMEPAGE_TOPHOMEPAGE_AFTER)
        a.href = adminlink
        a.class = "c_lnks"
        root.addchild(a)

        a = context.createControl(New a())
        a.text = context.string(stringId.HOMEPAGE_MAILTOME) + adminmail
        a.href = adminmail
        a.class = "c_lnks"
        root.addchild(a)
    End Sub

    Private Sub homepage_otherlinks(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not root Is Nothing, "root is a nothing xmlcontrol instance.")

        Dim otherlinks As vector(Of String) = Nothing
        Dim othertexts As vector(Of String) = Nothing
        If config.readlist(configNaming.urlshorten.name, configNaming.urlshorten.otherlink, _
                           otherlinks, context.generatefilters()) AndAlso _
           config.readlist(configNaming.urlshorten.name, configNaming.urlshorten.othertext, _
                           othertexts, context.generatefilters()) Then
            Dim i As Int64
            For i = 0 To max(otherlinks.size(), othertexts.size()) - 1
                Dim text As String = Nothing
                Dim link As String = Nothing
                text = othertexts(i)
                link = otherlinks(i)
                Dim a As a = Nothing
                a = context.createControl(New a())
                a.text = ifoper(isemptystring(text), context.string(stringId.HOMEPAGE_OTHERLINKS2_CONFIGERROR), text)
                a.href = ifoper(isemptystring(link), network.thislevelPathMark, link)
                a.class = "c_lnks"
                root.addchild(a)
            Next
        End If
    End Sub

    Private Sub urlkey_notfound(ByVal context As httpContext, ByVal page As xhtmlpage, ByVal url As String)
        assert(Not context Is Nothing, "context is a nothing httpContext instance.")
        assert(Not page Is Nothing, "page is a nothing xhtmlpage instance.")

        Dim xmltext As xmltext = Nothing
        xmltext = context.createControl(New xmltext())
        xmltext.text = context.string(stringId.URLKEYS_NOFOUND)
        page.body().addchild(xmltext)

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.text = url
        a.href = url
        page.body().addchild(a)
        page.body().addchild(context.createControl(New br()))

        delayJump_homepage(context, page)
    End Sub
End Class
